/*
* pps_bom
*/
package com.pitop.product.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.product.entity.Product;
import com.pitop.product.entity.ProductState;
import com.pitop.product.service.ProductSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 成品 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/product")
@Slf4j
@Api(value = "成品控制器", description = "成品控制器")
public class ProductCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ProductSV productSV;

    /**
     * 查询Product一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public Product load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Product product = productSV.load(id, code);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 查询Product一个详情信息，(将查询关联数据)
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public Product get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Product product = productSV.get(id, code);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 根据条件id查询Product一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public Product loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Product product = productSV.loadById(id);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 根据条件code查询Product一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public Product loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Product product = productSV.loadByCode(code);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 根据条件sku查询Product一个详情信息
     *
     * @param sku sku
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sku", value = "sku", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/sku/{sku}")
    @ResponseBody
    public Product loadBySku(@PathVariable String sku) {
        if (sku == null) {
            return null;
        }
        Product product = productSV.loadBySku(sku);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 根据条件sku查询Product一个详情信息
     *
     * @param upc upc
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "upc", value = "sku", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/upc/{upc}")
    @ResponseBody
    public Product loadByUpc(@PathVariable String upc) {
        if (upc == null) {
            return null;
        }
        Product product = productSV.loadByUpc(upc);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 根据条件id查询Product一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public Product getById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Product product = productSV.getById(id);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 根据条件code查询Product一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Product一个详情信息", notes = "查询Product一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public Product getByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Product product = productSV.getByCode(code);
        log.info(JSON.toJSONString(product));
        return product;
    }

    /**
     * 查询Product信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Product信息集合", notes = "查询Product信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Product> list(@RequestBody @ApiIgnore Product product, Integer curPage, Integer pageSize) {
        Page<Product> page = new Page<Product>(pageSize, curPage);
        List<Product> products = productSV.list(product, page.genRowStart(), page.getPageSize());
        int total = productSV.count(product);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return products;
    }

    /**
     * 统计Product信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Product信息数量", notes = "统计Product信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "alias", value = "成品别名", paramType = "query"),
            @ApiImplicitParam(name = "sku", value = "SKU", paramType = "query"),
            @ApiImplicitParam(name = "operateCode", value = "操作人编码", paramType = "query"),
            @ApiImplicitParam(name = "operateName", value = "操作人姓名", paramType = "query"),
            @ApiImplicitParam(name = "makerName", value = "制定人姓名", paramType = "query"),
            @ApiImplicitParam(name = "makerEmail", value = "制定人邮箱", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：[创建 Create,开发中 Developing,生产中 Producting,发货中 Delivery，完成 Done,取消 Cancel]", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "数量", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query"),
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Product product) {
        return productSV.count(product);
    }

    /**
     * 创建Product
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Product", notes = "创建Product")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alias", value = "成品别名", paramType = "query"),
            @ApiImplicitParam(name = "upc", value = "upc", paramType = "query"),
            @ApiImplicitParam(name = "sku", value = "SKU", paramType = "query"),
            @ApiImplicitParam(name = "makerName", value = "制定人姓名", paramType = "query"),
            @ApiImplicitParam(name = "makerEmail", value = "制定人邮箱", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "数量", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public Product build(@RequestBody @ApiIgnore Product product) {
        productSV.save(product);
        return product;
    }

    /**
     * 修改Product
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Product", notes = "修改Product")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "alias", value = "成品别名", paramType = "query"),
            @ApiImplicitParam(name = "upc", value = "upc", paramType = "query"),
            @ApiImplicitParam(name = "sku", value = "SKU", paramType = "query"),
            @ApiImplicitParam(name = "operateCode", value = "操作人编码", paramType = "query"),
            @ApiImplicitParam(name = "operateName", value = "操作人姓名", paramType = "query"),
            @ApiImplicitParam(name = "makerName", value = "制定人姓名", paramType = "query"),
            @ApiImplicitParam(name = "makerEmail", value = "制定人邮箱", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：[创建 Create,开发中 Developing,生产中 Producting,发货中 Delivery，完成 Done,取消 Cancel]", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "数量", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public Product modify(@RequestBody @ApiIgnore Product product) {
        productSV.modify(product);
        return product;
    }

    /**
     * 删除Product
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Product", notes = "删除Product")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        productSV.delete(id, code);
        return BeanRet.create(true, "删除Product成功");
    }

    /**
     * 修改成品状态
     *
     * @param code  编码
     * @param state 状态
     */
    @ApiOperation(value = "修改成品状态", notes = "修改成品状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, ProductState state) {
        productSV.updateByCode(code, state);
        return BeanRet.create(true, "修改成功");
    }
    /**
     * 复制成品
     *
     * @return BeanRet
     */
    @ApiOperation(value = "复制成品", notes = "复制成品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping("/copy")
    @ResponseBody
    public BeanRet copy( String code) {
        if ( StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        productSV.copy( code);
        return BeanRet.create(true, "复制成功");
    }

}
